package com.warmlight.voicepacket.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.AllPlayCoverRCAdapter;
import com.warmlight.voicepacket.data.AllplayerData;
import com.warmlight.voicepacket.http.Callback;
import com.warmlight.voicepacket.http.HttpClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;

import static com.warmlight.voicepacket.http.HttpConfig.COVER;
import static com.warmlight.voicepacket.http.HttpConfig.SERVER;

public class CoverListFragment extends Fragment {

//    private FooterListView footerListView;
//    private AllPlayerAdapter adapter=null;
//    private  boolean ISBODAN=true;//播单页面
    private List<AllplayerData.DataBean.ListBean> listBean;
    private  List<AllplayerData.DataBean.ListBean> datas;
//    public static final int UPDATE = 0x1;
    private int page=1;
//    private AllplayerData playdeatalData;
    private String category_id;
    private String type;
    private String pushtitle;
    private AllPlayCoverRCAdapter adapter1;
    private SmartRefreshLayout refreshLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_cover_list,container,false);

        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null){
            category_id = arguments.getString("category_id");
            type = arguments.getString("type");
            pushtitle = arguments.getString("pushtitle");
        }
        initView(view);
        initData();
    }


    private void initView(View view) {

//        footerListView = (FooterListView) view.findViewById(R.id.flv_play);
//
//
//        footerListView.setOnLastItemVisiableListener(new FooterListView.LastItemVisiableListener() {
//            @Override
//            public void onLastItemVisiable() {
//                page++;
//                initData();
//            }
//        });
        datas=new ArrayList<>();
//        adapter = new AllPlayerAdapter(getActivity(),datas , getActivity().getSupportFragmentManager(),ISBODAN,type);
//        footerListView.setAdapter(adapter);

        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                initData();
//                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        refreshLayout.setEnableAutoLoadMore(true);

        RecyclerView rc_list = view.findViewById(R.id.rc_list);
        rc_list.setLayoutManager(new GridLayoutManager(getContext(),2));
        adapter1 = new AllPlayCoverRCAdapter(getContext(),datas,type,pushtitle,category_id);
        rc_list.setAdapter(adapter1);

    }

    public void refresh(String cover_id){
        if (adapter1 != null){
            adapter1.refresh(cover_id);
        }
    }

    private void initData() {
        final TreeMap<String, String> list=new TreeMap<>();
        list.put("category_id",category_id);
        list.put("page",String.valueOf(page));
        list.put("pagesize","10");

        HttpClient.post(SERVER+COVER, list, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                page--;
                refreshLayout.finishLoadMore(false);
            }

            @Override
            public void onResponse(Call call, String response)   {
                Gson gson = new Gson();
//                String objst=response.body().string();
                AllplayerData playdeatalData = gson.fromJson(response, AllplayerData.class);
                listBean= playdeatalData.getData().getList();
                if (listBean.size()>0){


                    for (int j = 0;j<listBean.size();j++){
                        datas.add(listBean.get(j));
                    }

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (listBean.size()==0){
//                        footerListView.getFooterView().setVisibility(View.GONE);
//                                footerListView.setResult(FooterListView.ActionResult.NO_MORE);
                                refreshLayout.setNoMoreData(true);
                            }
                            if (adapter1!=null){
                                adapter1.notifyDataSetChanged();
                            }
                            refreshLayout.finishLoadMore(true);
                        }
                    });
//                    Message msg = new Message();
//                    msg.what = UPDATE;
//                    mHandler.sendMessage(msg);
                }else{
//                    Message msg = new Message();
//                    msg.what = UPDATE;
//                    mHandler.sendMessage(msg);

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (listBean.size()==0){
//                        footerListView.getFooterView().setVisibility(View.GONE);
//                                footerListView.setResult(FooterListView.ActionResult.NO_MORE);
                                refreshLayout.setNoMoreData(true);
                            }
                            if (adapter1!=null){
                                adapter1.notifyDataSetChanged();
                            }
                            refreshLayout.finishLoadMore(true);
                        }
                    });
                }

            }
        });
    }

//    private Handler mHandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//                case UPDATE:
//                    if (listBean.size()==0){
////                        footerListView.getFooterView().setVisibility(View.GONE);
//                        footerListView.setResult(FooterListView.ActionResult.NO_MORE);
//                    }
//                    if (adapter!=null){
//                        adapter.notifyDataSetChanged();
//                    }
//                    break;
//            }
//
//        }
//    };
}
